package com.baizhi.controller;


import com.baizhi.config.log.LogAnnotation;
import com.baizhi.domain.Result;
import com.baizhi.domain.StatusCode;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.CmfzUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljh
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/cmfzUser")
public class CmfzUserController {

    @Autowired
    private CmfzUserService cmfzUserService;

    /**
     * 模糊+分页查询
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @LogAnnotation(content = "分页加模糊查询用户",type = "select")
    @RequestMapping("/selectUserByPage")
    public Map<String,Object> selectUserByPage(Integer page,Integer limit,
                                               @RequestParam(required = false) String key){
        HashMap<String, Object> map = new HashMap<>();

        try{
            PageInfo<CmfzUser> pageInfo=cmfzUserService.selectUserByPageAndCondition(page,limit,key);
            System.out.println(pageInfo.getList());
            map.put("code",0);
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","查询失败");
        }
        return map;
    }

    @RequestMapping("/showUserSex")
    public Result showUserSex(){
        List<Map> list = cmfzUserService.selectUserSex();
        return new Result(true, StatusCode.OK,"OK",list);
    }

    @RequestMapping("/showUserCountByDate")
    public Result showUserCountByDate(){
        List<Map> list = cmfzUserService.selectUserCountByDate();
        HashMap<Object, Object> hashMap = new HashMap<>();

        ArrayList week = new ArrayList();
        ArrayList counts = new ArrayList();

        //ArrayList<Integer> list1 = new ArrayList<>();
        week.add("一周");
        week.add("两周");
        week.add("三周");

        if (list.size()<week.size()){
            int n=week.size()-list.size();
            for (int i = 0; i < n; i++) {
                counts.add(0);
            }
        }
        for (Map map : list) {

            System.out.println(map);
            System.out.println("----"+map.get("value"));
            counts.add(map.get("value"));
        }
        System.out.println(list);
        hashMap.put("week",week);
        hashMap.put("counts",counts);
        System.out.println(hashMap);
        return new Result(true, StatusCode.OK,"OK",hashMap);
    }

    @RequestMapping("/showUserChina")
    public Result showUserChina(){
        List<Map> list = cmfzUserService.selectUserChina();
        return new Result(true, StatusCode.OK,"OK",list);
    }

    @LogAnnotation(content = "修改用户的状态",type = "update")
    @RequestMapping("/updateUserStatus")
    public Result updateUserStatus(CmfzUser cmfzUser){
        try {
            cmfzUserService.updateById(cmfzUser);
            return new Result(true, StatusCode.OK,"修改成功");
        }catch (Exception e){
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
}
